package net.jjjshop.common.service.lbt.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.jjjshop.common.entity.lbt.CancelQueeSearchRequest;
import net.jjjshop.common.entity.lbt.LbPublicUpCancelEntity;
import net.jjjshop.common.entity.order.Order;
import net.jjjshop.common.mapper.lbt.LbPublicUpCancelMapper;
import net.jjjshop.common.service.lbt.LbPublicUpCancelService;
import net.jjjshop.common.service.order.OrderProductService;
import net.jjjshop.common.service.order.OrderService;
import net.jjjshop.framework.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("LbPublicUpCancelService")
public class LbPublicUpCancelServiceImpl  extends ServiceImpl<LbPublicUpCancelMapper, LbPublicUpCancelEntity> implements LbPublicUpCancelService {

    @Autowired
    private LbPublicUpCancelMapper dao;

    @Autowired
    private OrderService storeOrderService;
    @Autowired
    private OrderProductService storeOrderInfoService;
    @Override
    public Page<LbPublicUpCancelEntity> getPageList(CancelQueeSearchRequest request, Page<LbPublicUpCancelEntity> page) {
        Page<LbPublicUpCancelEntity> listData = dao.getPageListData(page,request);

        return listData;
    }
    @Override
    public Boolean updateStatus(LbPublicUpCancelEntity request) {
        LbPublicUpCancelEntity find = getById(request.getId());
        if (ObjectUtil.isNull(find)) {
            throw new BusinessException("记录不存在");
        }
        Integer status = find.getStatus();
        if (!status.equals(0)){
            throw new BusinessException("申请已处理过了");
        }
        //审核通过
        if (request.getStatus().equals(1)) {
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.eq("order_id", find.getOrderId());
            Order storeOrder = storeOrderService.getOne(wrapper);

            if (ObjectUtil.isNull(storeOrder)) {
                throw new BusinessException("订单记录不存在");
            }
//            storeOrder.setPaid(true);
//            storeOrder.setStatus(1);
//            BigDecimal freight = request.getFreight();
//            //大于0需要支付邮费
//            if (freight.compareTo(BigDecimal.ZERO) > 0) {
//                storeOrder.setPaid(false);
//                storeOrder.setStatus(0);
//                storeOrder.setPayPostage(freight);
//            }
//            // 生成订单号
//            String orderNo = CrmebUtil.getOrderNo("order");
//            storeOrder.setOrderId(orderNo);
//            storeOrder.setRealName(find.getRealName());
//            storeOrder.setUserPhone(find.getUserPhone());
//            storeOrder.setUserAddress(find.getUserAddress());
//            storeOrder.setId(null);
//            storeOrder.setCreateTime(null);
//            storeOrderService.create(storeOrder);
//
//            List<StoreOrderInfo> listData = storeOrderInfoService.getListByOrderNo(find.getOrderId());
//            List<StoreOrderInfo> storeOrderInfos = new ArrayList<>();
//            for (StoreOrderInfo detailVo : listData) {
//                // 订单详情
//                StoreOrderInfo soInfo = new StoreOrderInfo();
//                soInfo.setOrderId(storeOrder.getId());
//                soInfo.setProductId(detailVo.getProductId());
//                soInfo.setInfo(JSON.toJSON(detailVo).toString());
//                soInfo.setUnique(detailVo.getAttrValueId().toString());
//                soInfo.setOrderNo(orderNo);
//                soInfo.setProductName(detailVo.getProductName());
//                soInfo.setAttrValueId(detailVo.getAttrValueId());
//                soInfo.setImage(detailVo.getImage());
//                soInfo.setSku(detailVo.getSku());
//                soInfo.setPrice(detailVo.getPrice());
//                soInfo.setPayNum(detailVo.getPayNum());
//                soInfo.setWeight(detailVo.getWeight());
//                soInfo.setVolume(detailVo.getVolume());
//                soInfo.setCompanyId(detailVo.getCompanyId());
//                if (ObjectUtil.isNotNull(detailVo.getGiveIntegral()) && detailVo.getGiveIntegral() > 0) {
//                    soInfo.setGiveIntegral(detailVo.getGiveIntegral());
//                } else {
//                    soInfo.setGiveIntegral(0);
//                }
//                soInfo.setIsReply(false);
//                soInfo.setIsSub(detailVo.getIsSub());
//                soInfo.setProductType(detailVo.getProductType());
//                if (ObjectUtil.isNotNull(detailVo.getVipPrice())) {
//                    soInfo.setVipPrice(detailVo.getVipPrice());
//                } else {
//                    soInfo.setVipPrice(detailVo.getPrice());
//                }
//                soInfo.setRate(detailVo.getRate());
//                storeOrderInfos.add(soInfo);
//            }
//            storeOrderInfoService.saveOrderInfos(storeOrderInfos);
//        }
//
//        LbPublicUpCancelEntity updateEntity = new LbPublicUpCancelEntity();
//        updateEntity.setId(request.getId());
//        updateEntity.setStatus(request.getStatus());
//        updateEntity.setFreight(request.getFreight());
//        if(this.updateById(updateEntity)){
//            return true;
        }
        return false;
    }
}
